/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.dataacesslayer;

import mnstudent.datatranferobject.MajorDTO;
import java.sql.ResultSet;

/**
 *
 * @author 11520_000
 */
public class MajorDAL {
     
    //<editor-fold defaultstate="collapsed" desc="Attributes">
    private MajorDTO m_MajorObj;
    private DataExecuteUseStoreProcedure m_DataExecute;
    private String  m_sqlCmd;
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Method">
    public MajorDAL() {
        this.m_MajorObj = new MajorDTO();
        this.m_DataExecute = new DataExecuteUseStoreProcedure();    
        this.m_sqlCmd = "";
    }

    /**
     * Ham lay tat ca cac nganh
     *
     * @return boolean
     */
    public ResultSet getAllMajor() {
       try
        {
            m_sqlCmd = "{Call GetAllMajorFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,null);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getAllMajorWithNameOfFaculty()
    {
        try
        {
            m_sqlCmd = "{Call GetAllMajorWithFacultyFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,null);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getMajorById(String _id)
    {
        try {
            m_sqlCmd = "{Call GetMajorByIdFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getNameOfFacultyByMajorIdFromDatabase(String _idMajor)
    {
        try {
            m_sqlCmd = "{Call GetNameOfFacultyByMajorIdFromDatabase(?)}";
            String[] arrId = {_idMajor};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getAllMajorByFacultyID(String _facultyID){
            try {
            m_sqlCmd = "{Call GetAllMajorByFacultyID(?)}";
            String[] arrId = {_facultyID};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
     /**
     * ham them moi khoa xuong database
     * @param _major
     * @return true: success, false: fail
     */
    public boolean insertMajorToDatabase(MajorDTO _major)
    {
       try {
           m_sqlCmd = "{Call InsertMajorToDatabase(?,?,?)}";
           String[] arrStr = {_major.getFacultyID(), _major.getNameOfMajor(), _major.getFacultyID()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _major
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateMajorToDatabase(MajorDTO _major)
    {
       try {
           m_sqlCmd = "{Call UpdateMajorToDatabase(?,?,?)}";
           String[] arrStr = {_major.getMajorID(),  _major.getNameOfMajor(), _major.getFacultyID()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Ham xoa mot khoa ra khoi database
     * @param _major
     * @return 
     */
     public boolean deleteMajorFromDatabase(MajorDTO _major)
    {
        try {
           m_sqlCmd = "{Call DeleteMajorFromDatabase(?)}";
           String[] arrStr = {_major.getMajorID()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    //</editor-fold>
}
